package com.hmall.cart.listener;

import cn.hutool.json.JSONUtil;
import com.hmall.cart.service.ICartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class TradeListener {

    private final ICartService cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "cart.clear.queue", durable = "true"),
            exchange = @Exchange(value = "trade.topic",type = "topic"),
            key = "order.create"
    ))
    public void tradeListener(Message message, Long userId) {
        String itemids = message.getMessageProperties().getHeader("itemids");
        List<Long> list = JSONUtil.toList(itemids, Long.class);
        cartService.clear(userId, list);
        log.info("清除购物车成功，userId:{}", userId);
    }

}
